package com.hy.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.medicine.MedicineBasicInfo;
import com.hy.medicine.MedicineStock;
import com.hy.medicine.dto.StockQueryDTO;
import com.hy.medicine.dto.StockRecordQueryDTO;
import com.hy.medicine.vo.MedicineStockRecordVO;
import com.hy.medicine.vo.MedicineStockVO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【medicine_stock(药品库存表)】的数据库操作Mapper
* @createDate 2025-03-11 19:16:10
* @Entity com.hy.medicine.pojo.MedicineStock
*/
@Mapper
public interface MedicineStockMapper extends BaseMapper<MedicineStock> {

    /**
     * 根据药品ID查询库存信息
     */
    @Select("SELECT * FROM medicine_stock WHERE medicine_id = #{medicineId}")
    MedicineStock selectByMedicineId(@Param("medicineId") Integer medicineId);


    /**
     * 统计库存预警数量
     */
    @Select("SELECT COUNT(*) FROM medicine_stock ms " +
            "LEFT JOIN medicine_basic_info m ON ms.medicine_id = m.medicine_id " +
            "WHERE ms.stock_quantity <= m.minStock")
    Integer countWarning();

    /**
     * 统计总库存数量
     */
    @Select("SELECT COALESCE(SUM(stock_quantity), 0) FROM medicine_stock")
    Integer sumTotalQuantity();

    /**
     * 批量更新库存
     */
    int batchUpdateStock(@Param("list") List<MedicineStock> stockList);

    /**
     * 批量插入库存记录
     */
    int insertBatch(@Param("list") List<MedicineStock> stockList);

    /**
     * 批量更新库存记录
     */
    int updateBatchById(@Param("list") List<MedicineStock> stockList);



    /**
     * 查询库存变动记录（带药品信息）
     */
    Page<MedicineStockVO> selectStockRecords(Page<?> page, @Param("dto") StockQueryDTO dto);

}
